package dca.sistemascontrole.view.components;

import dca.sistemascontrole.util.ControleConstantes;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;

public class SensorAltura extends javax.swing.JPanel {

    private float porcentagemFluido = 1f;
    private static final int LINE_WIDTH = 3;
    private static final Color COR_AGUA = new Color(153, 217, 234);

    public SensorAltura() {
        initComponents();
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);

        int width = getWidth();
        int height = getHeight();

        int y = (int) (height - height * porcentagemFluido);
        if (y < 0) {
            y = 0;
        }
        g.setColor(COR_AGUA);
        g.fillRect(0, y, width, height);

        y = 0;
        g.setColor(Color.BLACK);
        for (int i = 0; i < 10; i++) {
            g.drawLine(0, y, LINE_WIDTH, y);

            y += height / 10;
        }

        g.drawRect(0, 0, width - 1, height - 1);

        Graphics2D g2d = (Graphics2D) g;
        String nivel = String.format("%.2f cm", (ControleConstantes.ALTURA_TANQUE * porcentagemFluido));
        Font font = new Font(nivel, Font.PLAIN, 10);
        g2d.setFont(font);

        Rectangle2D stringBounds = font.getStringBounds(nivel, g2d.getFontRenderContext());
        int textWidth = (int) stringBounds.getWidth();
        int textHeight = g2d.getFontMetrics().getHeight();

        g2d.drawString(nivel, (width / 2 - textWidth / 2), (height / 2 - textHeight / 2));
    }

    public float getPorcentagemFluido() {
        return porcentagemFluido;
    }

    public void setPorcentagemFluido(float porcentagemFluido) {
        this.porcentagemFluido = porcentagemFluido;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 41, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
